/*************************************************
 *FileName:      security.js
 *Description:   设置密保问题.js
 *Others:        陈灿
 *************************************************/
    /*
     * 启动
     */
    mui.init();
    mui('.mui-scroll-wrapper').scroll({
        indicators: false,      //是否显示滚动条 默认为true
        deceleration: 0.003,    //阻尼系数,系数越小滑动越灵敏 默认0.0006
        bounce: true           //是否启用回弹 默认true
    });
    mui.init({
        beforeback: function() {
            var list = plus.webview.currentWebview().opener();
            mui.fire(list, 'rssecurity');
            return true;
        }
    });
    /*
     * 变量初始化
     */
    var uid = localStorage.replace_uid;
    var token = localStorage.replace_token
    var security_question,security_answer;
    var ostate,osecurity_answer;
    /*
     * 主程序
     */ 
    $(document).ready(function() 
    {
        //样式加载
        pagecss();
        //密保问题
        question();
        state();
    });
    /**
     * pagecss
     * 样式加载
     * @cc 
     * @DateTime 2018-01-08T06:19:16+0800
     * @param    int        
     * @return   void
     */
    function pagecss(){
        $('.mui-popover').on('tap','.mui-table-view-cell',function(){
            $('.question').find('input').val($(this).find('a').text()) ;
            $('.mui-popover').removeClass('mui-active');
            $('.mui-popover').css({display:'none'});
            $('.mui-backdrop').remove();
            security_question=$(this).attr('data-id');
        })
        
        $('.rdntn').on('tap',function(){
            if(ostate=='2'){
                security_answer =$('.answer input').val();
                uplode();
            }else{
                if($('.answer input').val()==osecurity_answer){
                    $('.question').attr('href','#middlePopover');
                    mui.toast('密码正确！',{ duration:2000, type:'div' });
                    $('.mui-title').text('设置密保问题')
                    $('.questions').val('');
                    $('.answers').val('');
                    ostate='2';
                }else{
                    mui.toast('密码错误！',{ duration:2000, type:'div' });
                }
            }
            
        })
        
    }
    /**
     * 判断用户状态 
     * 
     * @cc 
     * @DateTime 2018-01-08T06:19:16+0800
     * @param    int        
     * @return   void
     */
    function state(){
        $.ajax({
            type: "post",
            url: DOMAIN_NAME + "/index.php/api/user/usersAnswer",
            dataType:'jsonp',
            data: { uid: uid, token: token,},
            success: function(data) {
                if(data.error_code == 0){
                    ostate='1';
                    $('.mui-title').text('确认密保问题')
                    $('.question').removeAttr('href');
                    $('.questions').val(data.data.security_question);
                    osecurity_answer = data.data.security_answer;
                }else if(data.error_code == 1001){
                    setTimeout(function(){
                        exitlogin();
                    },1000);
                }else{
                    ostate='2';
                    $('.mui-title').text('设置密保问题')
                }
            }
        }); 
    }
    /**
     * 上传
     * 
     * @cc 
     * @DateTime 2018-01-08T06:19:16+0800
     * @param    int        
     * @return   void
     */
    function uplode(){
        $.ajax({
            type: "post",
            url: DOMAIN_NAME + "/index.php/api/user/setSecurityQuestion",
            dataType:'jsonp',
            data: { uid: uid, token: token,security_question:security_question,security_answer:security_answer,},
            success: function(data) {
                if(data.error_code == 0){
                    mui.toast(data.error_msg,{ duration:2000, type:'div' });
                    setTimeout(function(){mui.back()},500);
                }else if(data.error_code == 1001){
                    setTimeout(function(){
                        exitlogin();
                    },1000);
                }else{
                    mui.toast(data.error_msg,{ duration:2000, type:'div' });
                }
            }
        }); 
    }
    /**
     * 密保问题
     * 
     * @cc 
     * @DateTime 2018-01-08T06:19:16+0800
     * @param    int        
     * @return   void
     */
    function question(){
        $.ajax({
            type: "post",
            url: DOMAIN_NAME + "/index.php/api/user/securityQuestion",
            dataType:'jsonp',
            data: { uid: uid, token: token,},
            success: function(data) {
                if(data.error_code == 0){
                    var ohtml='';
                    for(var i=0 ; i<data.data.length;i++){
                        ohtml+='<li class="mui-table-view-cell" data-id="'+data.data[i].id+'"><a>'+data.data[i].content+'</a></li>'
                    }
                    $('.mui-scroll-wrapper .mui-table-view').html(ohtml);
                }else if(data.error_code == 1001){
                    setTimeout(function(){
                        exitlogin();
                    },1000);
                }else{
                    mui.toast(data.error_msg,{ duration:2000, type:'div' });
                }
            }
        }); 
    }
